<template>
    <div class="home">
    
        <van-nav-bar fixed title="新增订单" right-text="提交" @click-right="submitHandle"/>
        <div>
            <van-cell-group>
                <van-field required v-model="orderNum" clearable label="订单编号" placeholder="请输入订单编号"/>
                <van-cell
                        required
                        title="外商国别"
                        @click="clickHandle('foreignCountry')"
                        v-model="foreignCountry1.text"
                        is-link
                        value="请选外商国别"
                />
                <van-cell
                        required
                        title="订单日期"
                        @click="clickTimeHandle('orderTime')"
                        v-model="orderTime1"
                        is-link
                        value="请选择订单日期"
                />
                <van-cell
                        required
                        title="交货日期"
                        v-model="deliveryTime1"
                        @click="clickTimeHandle('deliveryTime')"
                        is-link
                        value="请选择交货日期"
                />
                <van-cell
                        required
                        title="抵运国"
                        @click="clickHandle('arrivalCountry')"
                        v-model="arrivalCountry1.text"
                        is-link
                        value="请选择抵运国"
                />
                <van-cell
                        required
                        v-model="currency1.text"
                        title="订单币种"
                        @click="clickHandle('orderCurrency')"
                        is-link
                        value="请选择订单币种"
                />
                <van-field required v-model="totalPrice" clearable label="成交总价" placeholder="请输入成交总价"/>

                <van-popup v-model="show" position="bottom" :overlay="true">
                    <van-picker @confirm="onConfirm" @cancel="onCancel" show-toolbar :columns="arrAll"/>
                </van-popup>

                <van-popup v-model="timeShow" position="bottom" :overlay="true">
                    <van-datetime-picker
                            @confirm="onTimeComfirm"
                            @cancel="onTimeCancel"
                            v-model="currentDate"
                            type="date"
                    />
                </van-popup>

                <van-cell
                        title="报关地海关"
                        v-model="declareCustoms1.text"
                        @click="clickHandle('declareCustoms')"
                        is-link
                        value="请选择报关地海关"
                />
                <van-cell
                        title="起运港"
                        @click="clickHandle('portLoading')"
                        v-model="portLoading1.text"
                        is-link
                        value="请选择起运港"
                />
                <van-cell
                        title="目的港"
                        @click="clickHandle('portDestination')"
                        v-model="portDestination1.text"
                        is-link
                        value="请选择目的港"
                />
                <van-cell
                        title="收汇方式"
                        @click="clickHandle('receiptMode')"
                        v-model="receiptMode1.text"
                        is-link
                        value="请选择收汇方式"
                />
                <van-cell
                        title="贸易方式"
                        @click="clickHandle('tradeMode')"
                        v-model="tradeMode1.text"
                        is-link
                        value="请选择贸易方式"
                />
                <van-cell
                        title="价格条款"
                        @click="clickHandle('priceClause')"
                        v-model="priceClause1.text"
                        is-link
                        value="请选择价格条款"
                />
                <van-cell
                        title="包装种类"
                        @click="clickHandle('packingMode')"
                        v-model="packingMode1.text"
                        is-link
                        value="请选择包装种类"
                />
                <van-cell
                        title="运输方式"
                        @click="clickHandle('transportMode')"
                        v-model="transportMode1.text"
                        is-link
                        value="请选择运输方式"
                />
            </van-cell-group>
            <van-cell-group>
                <van-field label="备注" v-model="remarks" type="textarea" placeholder="请输入备注"/>
            </van-cell-group>
            <van-cell-group>
                <van-field label="关联报关单" v-model="relevantDeclarations" type="textarea" placeholder="请输入备注"/>
            </van-cell-group>
            <div style="text-align: center">
                <van-button type="primary" @click="submitHandle" size="normal">提交</van-button>
            </div>
        </div>
    </div>
</template>

<script>
    // @ is an alias to /src
    import Qs from "qs";
    import { Dialog } from 'vant';


    import {NavBar, Cell, Field, Picker, DatetimePicker} from "vant";
    import country_data from '@/assets/data/country.json'
    import address_data from '@/assets/data/address.json'
    import address2_data from '@/assets/data/address2.json'
    import Vue from "vue";
    import axios from "axios";
    Vue.use(Dialog);

    var API = "http://47.96.125.159:8010";
    // var API = "http://uatorcompanyapi.dc.zjmade.cn";
    // var API =  "http://orcompanyapi.dc.zjmade.cn";

    Vue.use(NavBar)
        .use(Cell)
        .use(Picker)
        .use(DatetimePicker)
        .use(Field);

    export default {
        name: "home",
        data() {
            return {
                totalPrice:'',
                token:'',
                loading:false,
                orderNum: "",
                _target: "",
                baseUrl: "47.96.125.159:8010",
                timeShow: false,
                show: false,
                remarks: "",
                relevantDeclarations: "",
                foreignCountry1: "",
                arrivalCountry1: "",
                tradeMode1: "",
                receiptMode1: "",
                currency1: "",
                deliveryTime1: "",
                priceClause1: "",
                packingMode1: "",
                orderTime1: "",
                transportMode1: "",
                declareCustoms1: "",
                portLoading1: "",
                portDestination1: "",
                receiptMode: "",
                tradeMode: "",
                priceClause: "",
                packingMode: "",
                transportMode: "",
                orderTime: "",
                deliveryTime: "",
                deliveryTime: [],
                orderTime: [],
                transportMode: [],
                packingMode: [],
                priceClause: [],
                tradeMode: [],
                receiptMode: [],
                portDestination: [],
                portLoading: [],
                declareCustoms: [],
                foreignCountry: [],
                arrivalCountry: [],
                orderCurrency: [],
                orderCurrency: "",
                arrAll: [],
                currentDate: new Date()
            };
        },
        created() {
            this.token = this.$route.query.token;
            this.hscode = this.$route.query.hscode;
            // this.init_select_data()
            let tmp = {
                token:this.token,
                hscode:this.hscode
            }
                axios.post(API+'/api/1.0/token/check',Qs.stringify(tmp), {
                    headers: {
                        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
                    }
                }).then(res=>{
                   if(res.data.code !==0){
                       this.$dialog.alert({
                           message:'登录过期，请重新操作'
                       }).then(()=>{
                           location.href=`${API}/activate/${this.hscode}`;
                       })
                       return;
                   }
                })

            if(this.token){
                this.init_select_data();
            }else{
                return;
            }

        },
        methods: {
            submitHandle() {

                if (!this.orderNum || this.orderNum == "") {
                    this.$toast("请输入订单编号");
                    return;
                }
                if (!this.foreignCountry1 || this.foreignCountry1 == "") {
                    this.$toast("请选择外商国别");
                    return;
                }
                if (!this.orderTime1 || this.orderTime1 == "") {
                    this.$toast("请选择订单日期");
                    return;
                }
                if (!this.deliveryTime1 || this.deliveryTime1 == "") {
                    this.$toast("请选择交货日期");
                    return;
                }
                if (!this.totalPrice || this.totalPrice == "") {
                    this.$toast("请输入成交总价");
                    return;
                }
                // 提交数据....'
                this.loading = true;
                let _data = {
                    orderCodeCmp: this.orderNum,
                    foreignCountry: this.foreignCountry1.key,
                    orderTime: this.orderTime1,
                    deliveryTime: this.deliveryTime1,
                    arrivalCountry: this.arrivalCountry1.key,
                    orderCurrency: this.currency1.key,
                    declareCustoms: this.declareCustoms1.key,
                    portLoading: this.portLoading1.key,
                    portDestination: this.portDestination1.key,
                    receiptMode: this.receiptMode1.key,
                    tradeMode: this.tradeMode1.key,
                    priceClause: this.priceClause1.key,
                    packingMode: this.packingMode1.key,
                    transportMode: this.transportMode1.key,
                    applicationState: 0,// 0 暂存；1在手订单；2、申报订单
                    meno: this.remarks,
                    totalPrice:this.totalPrice,
                    products: [],
                    bf4: this.relevantDeclarations,
                    token: this.token
                }
                axios.post(API + '/api/1.0/company/order/upload', Qs.stringify(_data), {
                    headers: {
                        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
                    }
                }).then(res => {
                    if(res.data.code ==0){
                        // this.loading = false;
                        this.$dialog.alert({
                            message: '数据提交成功'
                        }).then(() => {
                            // on close
                            this.$router.go(0)
                        });
                    }else {

                    }

                });
            },
            clickHandle(type) {
                switch (type) {
                    case "foreignCountry":
                        this.arrAll = this.foreignCountry;
                        this._target = "foreignCountry1";
                        break;
                    case "orderCurrency":
                        this.arrAll = this.orderCurrency;
                        this._target = "currency1";
                        break;
                    case "arrivalCountry":
                        this.arrAll = this.arrivalCountry;
                        this._target = "arrivalCountry1";
                        break;
                    case "declareCustoms":
                        this.arrAll = this.declareCustoms;
                        this._target = "declareCustoms1";
                        break;
                    case "portLoading":
                        this.arrAll = this.portLoading;
                        this._target = "portLoading1";
                        break;
                    case "portDestination":
                        this.arrAll = this.portDestination;
                        this._target = "portDestination1";
                        break;
                    case "receiptMode":
                        this.arrAll = this.receiptMode;
                        this._target = "receiptMode1";
                        break;
                    case "tradeMode":
                        this.arrAll = this.tradeMode;
                        this._target = "tradeMode1";
                        break;
                    case "priceClause":
                        this.arrAll = this.priceClause;
                        this._target = "priceClause1";
                        break;
                    case "packingMode":
                        this.arrAll = this.packingMode;
                        this._target = "packingMode1";
                        break;
                    case "transportMode":
                        this.arrAll = this.transportMode;
                        this._target = "transportMode1";
                        break;
                    default:
                        break;
                }
                this.show = true;
            },
            clickTimeHandle(type) {
                switch (type) {
                    case "orderTime":
                        this.arrAll = this.orderTime;
                        this._target = "orderTime1";
                        break;
                    case "deliveryTime":
                        this.arrAll = this.deliveryTime;
                        this._target = "deliveryTime1";
                        break;
                    default:
                        break;
                }
                this.timeShow = true;
            },
            getCountry() {
                /*请求国家 */
                var arr = [];
                var data = country_data.data;
                for (let i = 0; i < data.length; i++) {
                    let map = {}
                    map.text = data[i].configValue;
                    map.key = data[i].configKey;
                    arr.push(map);
                }
                this.foreignCountry = arr;
                this.arrivalCountry = arr;
            },
            getReceiptMode() {
                /*请求收汇方式 */
                var tmp = {
                    configTypeId: 3009,
                    delFlag: "0",
                    pageLimit: 1000,
                    token:this.token
                };
                axios
                    .post(API + "/api/1.0/base/s901060parameter/list", Qs.stringify(tmp), {
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
                        }
                    })
                    .then(res => {
                        if (res.data.code == 0) {
                            let arr = [];
                            let data = res.data.data;

                            for (let i = 0; i < data.length; i++) {
                                let map = {};
                                map.text = data[i].configValue
                                map.key = data[i].configKey
                                arr.push(map);
                            }
                            this.receiptMode = arr;
                        }
                    });
            },
            getTransportMode() {
                /*运输方式*/
                var tmp = {
                    configTypeId: 3005,
                    delFlag: "0",
                    pageLimit: 1000,
                    token:this.token
                };
                axios
                    .post(API + "/api/1.0/base/s901060parameter/list", Qs.stringify(tmp), {
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
                        }
                    })
                    .then(res => {
                        if (res.data.code == 0) {
                            let arr = [];
                            let data = res.data.data;

                            for (let i = 0; i < data.length; i++) {
                                let map = {};
                                map.text = data[i].configValue
                                map.key = data[i].configKey;
                                arr.push(map);
                            }
                            this.transportMode = arr;
                        }
                    });
            },
            getPackingMode(){
                /*包装种类*/
                var tmp = {
                    configTypeId: 3011,
                    delFlag: "0",
                    pageLimit: 1000,
                    token:this.token
                };
                axios
                    .post(API + "/api/1.0/base/s901060parameter/list", Qs.stringify(tmp), {
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
                        }
                    })
                    .then(res => {
                        if (res.data.code == 0) {
                            let arr = [];
                            let data = res.data.data;

                            for (let i = 0; i < data.length; i++) {
                                let map = {};
                                map.text = data[i].configValue;
                                map.key = data[i].configKey;
                                arr.push(map);
                            }
                            this.packingMode = arr;
                        }
                    });
            },
            getPriceClause(){
                /*价格条款*/
                var tmp = {
                    configTypeId: 3004,
                    delFlag: "0",
                    pageLimit: 1000,
                    token:this.token
                };
                axios
                    .post(API + "/api/1.0/base/s901060parameter/list", Qs.stringify(tmp), {
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
                        }
                    })
                    .then(res => {
                        if (res.data.code == 0) {
                            let arr = [];
                            let data = res.data.data;

                            for (let i = 0; i < data.length; i++) {
                                let map = {};
                                map.text = data[i].configValue
                                map.key = data[i].configKey
                                arr.push(map);
                            }
                            this.priceClause = arr;
                        }
                    });
            },
            getTradeMode(){
                /*贸易方式 */
                var tmp = {
                    configTypeId: 3010,
                    delFlag: "0",
                    pageLimit: 1000,
                    token:this.token
                };
                axios
                    .post(API + "/api/1.0/base/s901060parameter/list", Qs.stringify(tmp), {
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
                        }
                    })
                    .then(res => {
                        if (res.data.code == 0) {
                            let arr = [];
                            let data = res.data.data;

                            for (let i = 0; i < data.length; i++) {
                                let map = {};
                                map.text = data[i].configValue
                                map.key = data[i].configKey
                                arr.push(map);
                            }
                            this.tradeMode = arr;
                        }
                    });},
            getPortDestination(){
                /*请求起运港 */
                var arr = [];
                var data = address_data.data;

                for (let i = 0; i < data.length; i++) {
                    let map = {};
                    map.text = data[i].wharfName;
                    map.key =data[i].wharfCode;
                    arr.push(map);
                }
                this.portLoading = arr;
                /*请求目的港 */
                this.portDestination = arr;
            },
            getOrderCurrency(){
                // 币种
                var tmp = {
                    configTypeId: 3001,
                    delFlag: "0",
                    pageLimit: 1000,
                    token:this.token
                };
                axios
                    .post(API + "/api/1.0/base/s901060parameter/list", Qs.stringify(tmp), {
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
                        }
                    })
                    .then(res => {
                        if (res.data.code == 0) {
                            let arr = [];
                            res.data.data.forEach(element => {
                                let map = {};
                                map.text =element.configValue;
                                map.key = element.configKey;
                                arr.push(map);
                            });
                            this.orderCurrency = arr;
                        }
                    });
            },
            getDeclareCustoms(){
                //报关地海关

                var data = address2_data.data;
                let arr = [];

                data.forEach(element => {
                    let map = {};
                    map.text = element.departmentName;
                    map.key = element.departmentCode;
                    arr.push(map);
                });
                this.declareCustoms = arr;



         /*       var tmp = {
                    // configTypeId: 3008,
                    delFlag: "0",
                    pageLimit: 5000,
                    token:this.token
                };
                axios
                    .post(API + "/api/1.0/base/m812050cbinfo/list", Qs.stringify(tmp), {
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
                        }
                    })
                    .then(res => {
                        if (res.data.code == 0) {
                            let arr = [];

                            res.data.data.forEach(element => {
                                let map = {};
                                map.text = element.departmentName;
                                map.key = element.departmentCode;
                                arr.push(map);
                            });
                            this.declareCustoms = arr;
                        }
                    });*/
            },
            init_select_data() {
                this.getCountry();
                this.getReceiptMode();
                this.getTransportMode();
                this.getPackingMode();
                this.getPriceClause();
                this.getTradeMode();
                this.getPortDestination();
                this.getOrderCurrency();
                this.getDeclareCustoms();

            },
            onConfirm(value) {
                let that = this;
                that[this._target] = value;
                that.show = false;
            },
            onCancel() {
                let that = this;
                that.show = false;
            },
            onTimeComfirm(value) {
                let that = this;
                that.value =
                    value.getFullYear() +
                    "-" +
                    (value.getMonth() + 1) +
                    "-" +
                    value.getDate();
                that[this._target] = that.value;

                that.timeShow = false;
            },
            onTimeCancel() {
                let that = this;
                that.timeShow = false;
            }
        }
    };
</script>

<style scoped>
    button {
        margin-top: 20px;
        min-width: 200px;
        background-color: #1989fa;
        border-color: #1989fa;
    }

    .home {
        padding-top: 60px;
        padding-bottom: 20px;
    }

    .van-nav-bar {
        /*background-color: #fff;*/
        /*color: #fff;*/
    }
    .van-nav-bar__right{
        color: #fff !important;
    }
    .van-nav-bar__text {
        color: #fff !important;
    }
</style>